package ingresso

class LoginController {

	def beforeInterceptor = [action:this.&checkSession,except:['index','form','doLogin']]
	
	def checkSession() {
		if(!session.usuario) {			
			redirect(action:'index', params: params)
			return false
		}else{
			return true
		}
	}
	
    def index() {		
		redirect(action: "form", params: params)
	}
	
	def form () {
		if(!session.usuario){			
			render(view:"login")
		}else{
			redirect(uri:"/");
		}
	}
	
	def doLogin () {
		def usuario = Usuario.findWhere(email:params['email'],senha:params['senha'])
		
		session.usuario = usuario
		if(usuario){
			redirect(uri:"/");
		}else{
			flash.message = message(code: 'usuario.login.error.message', args: [message(code: 'login.label', default: 'Login')])
			redirect(action:'form')
		}
				
	}
	
	def doLogout () {
		session.usuario = null
		redirect(action:'form')
	}
}
